package com.learn.annotation_config.value.beans.dao;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("userDao")
public class UserDaoImpl implements UserDao {
    // 1️⃣字段注入普通字符串
    @Value("172.0.0.1")
    private String ip;
    
    // 2️⃣方法注入普通字符串
    private String port;
    
    @Value("8080")
    public void setPort(String port) {
        this.port = port;
    }
    
    // 3️⃣注入spEL表达式，支持运算
    @Value("#{T(java.lang.Runtime).getRuntime().availableProcessors()}")
    private int minCons;
    @Value("#{T(java.lang.Runtime).getRuntime().availableProcessors()*2}")
    private int maxCons;
    
    // 4️⃣注入配置文件内的配置值
    //      配置文件需要在启动类上通过@PropertySource({"classpath:xxx"})注解添加配置文件
    //      Spring boot项目的yml配置文件无需添加@PropertySource({"classpath:xxx"})注解
    @Value("${my-app.username}")
    private String username;
    @Value("${my-app.password}")
    private String password;
    
    public void show() {
        System.out.println("ip:" + ip);
        System.out.println("port:" + port);
        System.out.println("minCons:" + minCons);
        System.out.println("maxCons:" + maxCons);
        System.out.println("username:" + username);
        System.out.println("password:" + password);
    }
}
